package com.sanjiahantang.ythshopping.activity.me;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sanjiahantang.ythshopping.R;
import com.sanjiahantang.ythshopping.activity.BaseActivity;
import com.sanjiahantang.ythshopping.adapter.home.PayTypeAdapter;
import com.sanjiahantang.ythshopping.config.ConfigValue;
import com.sanjiahantang.ythshopping.model.Address;
import com.sanjiahantang.ythshopping.model.Bouns;
import com.sanjiahantang.ythshopping.model.CartGoods;
import com.sanjiahantang.ythshopping.model.CreateOrder;
import com.sanjiahantang.ythshopping.model.CreateOrderModel;
import com.sanjiahantang.ythshopping.model.DillBean;
import com.sanjiahantang.ythshopping.model.OrderGlist;
import com.sanjiahantang.ythshopping.model.SubmitOrderModel;
import com.sanjiahantang.ythshopping.presenter.CreateOrderPresenter;
import com.sanjiahantang.ythshopping.presenter.SubmitOrderPresenter;
import com.sanjiahantang.ythshopping.utils.Utils;
import com.sanjiahantang.ythshopping.views.CreateOrderView;
import com.sanjiahantang.shopping.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.shopping.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.shopping.uilibrary.views.MyListView;
import com.squareup.picasso.Picasso;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by sks on 2015/10/8.
 * <p>
 * 生成订单
 */
public class CreateOrderActivity extends BaseActivity implements View.OnClickListener,
        CreateOrderView {
    TextView title;
    ImageView back;
    private Context context;
    private Address aModel = new Address();
    private Bouns bModel = new Bouns();
    private TextView txtDistribution, txtFee, txtName, txtPhone, txtAddress,
            txtGoodPrice, txtTotal, txtButton, txtSubmit,
            tv_transportDetail;
    private MyListView lv_paytype;
    private EditText editContent;
    private LinearLayout ll_expenseshow, ll_expensein;
    private ImageView iv_expense;
    private EditText et_expenseV;
    private TextView tv_expenseall;
    private LinearLayout ll_hoss, ll_hossway, ll_hosswait, ll_hosscancle, ll_hosschat;
    private ImageView iv_hosswait, iv_hosscancle, iv_hosschat;
    private TextView tv_instr,tv_band, tv_extre;
    private TextView tv_dill;
    private RelativeLayout rl_dill;
    private TextView tv_dillorice;
    private TextView giveIntegral;
    private TextView giveBand;


    private LinearLayout ll_goods;
    public static final int DISTRIBUTION = 1;
    public static final int ADDRESS = 2;
    public static final int ENVELOPE = 3;
    public static final int CANCLE_ENVELOPE = 4;
    private CreateOrderPresenter coPresenter;
    private SubmitOrderPresenter soPresenter;


    private QuickAdapter<CartGoods> quickAdapter;
    private boolean freight = false;
    private String addressID = "";
    private int paytypenum = -1;
    int paytypeid = -1;

    private String itType;
    //运费
    private double fee = 0;

    //积分
    private double integral = 0;
    //消费券
    private double band = 0;

    //消费积分
    private boolean exporseShow = false;
    //是否有海外商品
    private boolean oversea = false;
    private String overseaStr = "";


    private double exporse = 0;

    private boolean hossShow = false;
    private boolean hasdill = false;
    private int hossway = 0;

    private double totleprice = 0;

    private String pwd = "";

    private DillBean bean;

    @Override
    public void initView() {
        super.initView();
        setContentView(R.layout.activity_createorder);
        context = this;

        itType = getIntent().getStringExtra("intentType");
        coPresenter = new CreateOrderPresenter(this);
        soPresenter = new SubmitOrderPresenter(this);
        title = (TextView) findViewById(R.id.title);
        back = (ImageView) findViewById(R.id.back);
        back.setBackgroundResource(R.mipmap.ic_back);
        title.setText("确认订单");
        findViewById(R.id.fl_Left).setOnClickListener(this);
        txtDistribution = (TextView) findViewById(R.id.txtDistribution);
        tv_transportDetail = (TextView) findViewById(R.id.tv_ogitem_transportdetail);
        txtFee = (TextView) findViewById(R.id.txtFee);
        txtName = (TextView) findViewById(R.id.txtName);
        txtPhone = (TextView) findViewById(R.id.txtPhone);
        txtAddress = (TextView) findViewById(R.id.txtAddress);
        txtButton = (TextView) findViewById(R.id.txtButton);
        txtSubmit = (TextView) findViewById(R.id.txtSubmit);
        editContent = (EditText) findViewById(R.id.editContent);
        ll_goods = (LinearLayout) findViewById(R.id.ll_CQA_goods);
        ll_expenseshow = (LinearLayout) findViewById(R.id.ll_creatoeder_expenseshow);
        ll_expensein = (LinearLayout) findViewById(R.id.ll_creatoeder_expensein);
        iv_expense = (ImageView) findViewById(R.id.iv_creatoeder_expensein);
        tv_expenseall = (TextView) findViewById(R.id.tv_creatoeder_expensein);
        et_expenseV = (EditText) findViewById(R.id.et_reatoeder_expensein);
        lv_paytype = (MyListView) findViewById(R.id.lv_creatorder_paytype);
        ll_hoss = (LinearLayout) findViewById(R.id.ll_creatorder_hoss);
        ll_hossway = (LinearLayout) findViewById(R.id.ll_creatorder_hossway);
        ll_hosswait = (LinearLayout) findViewById(R.id.ll_creatoeder_hosswait);
        ll_hosscancle = (LinearLayout) findViewById(R.id.ll_creatoeder_hosscancle);
        ll_hosschat = (LinearLayout) findViewById(R.id.ll_creatoeder_hosschar);
        iv_hosschat = (ImageView) findViewById(R.id.iv_creatoeder_hosschar);
        iv_hosswait = (ImageView) findViewById(R.id.iv_creatoeder_hosswait);
        iv_hosscancle = (ImageView) findViewById(R.id.iv_creatoeder_hosscancle);
        tv_instr = (TextView) findViewById(R.id.tv_creatorder_instr);
        tv_band = (TextView) findViewById(R.id.tv_creatorder_band);
        tv_extre = (TextView) findViewById(R.id.tv_creatorder_exstr);
        tv_dill = (TextView) findViewById(R.id.tv_createorder_dill);
        rl_dill = (RelativeLayout) findViewById(R.id.rlDill);
        tv_dillorice = (TextView) findViewById(R.id.txtDill);
        giveIntegral = (TextView) findViewById(R.id.give_integral);
        giveBand = (TextView) findViewById(R.id.give_band);

        txtGoodPrice = (TextView) findViewById(R.id.txtGoodPrice);
        txtTotal = (TextView) findViewById(R.id.txtTotal);
        findViewById(R.id.layoutAddress).setOnClickListener(this);

        txtSubmit.setOnClickListener(this);
        ll_expenseshow.setOnClickListener(this);
        ll_hoss.setOnClickListener(this);
        ll_hosscancle.setOnClickListener(this);
        ll_hosswait.setOnClickListener(this);
        ll_hosschat.setOnClickListener(this);
        tv_dill.setOnClickListener(this);


        if (itType.equals("1")) {
            coPresenter.loadOrderInforGoods(context, getIntent().getStringExtra("Carid"));
        } else {
            coPresenter.loadOrderInfor(context);
        }

        ChHossWay(0);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if (resultCode != RESULT_OK) {
                    Bundle b = new Bundle();
                    if (null == data) {
                        return;
                    }
                    b = data.getExtras();
                    switch (resultCode) {

                        //接收地址
                        case ADDRESS:
                            aModel = (Address) b.getSerializable("orderaddress");
                            addressID = aModel.getAddress_id();
                            txtName.setText(aModel.getUsername());
                            txtPhone.setText(aModel.getTelnumber());
                            txtAddress.setText(aModel.getAddress());
                            break;


                    }
                }
                break;
            case 2:
                if (resultCode != 0) {
                    if (data.getStringExtra("open").equals("0")) {
                        rl_dill.setVisibility(View.GONE);
                        hasdill = false;
                    } else {

                        hasdill = true;
                        bean = (DillBean) data.getSerializableExtra("dillbean");
                        if (bean.getRate().equals("0")) {
                            rl_dill.setVisibility(View.GONE);

                            if (exporseShow) {
                                BigDecimal c = new BigDecimal(totleprice + fee - exporse - integral * Double.valueOf(coModel.getTotal().getIntegral_scale()) - band * Double.valueOf(coModel.getTotal().getBond_scale()));
                                txtTotal.setText(String.valueOf(c.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()));
                            } else {
                                BigDecimal c = new BigDecimal(totleprice + fee - integral * Double.valueOf(coModel.getTotal().getIntegral_scale()) - band * Double.valueOf(coModel.getTotal().getBond_scale()));
                                txtTotal.setText(String.valueOf(c.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()));
                            }
                        } else {
                            rl_dill.setVisibility(View.VISIBLE);
                            BigDecimal b = new BigDecimal(Double.valueOf(coModel.getTotal().getGoods_price_formated()) * Double.valueOf(bean.getRate()) / 100);

                            tv_dillorice.setText(b.setScale(1, BigDecimal.ROUND_HALF_UP).toString());

                            if (exporseShow) {
                                BigDecimal c = new BigDecimal(b.doubleValue() + totleprice + fee - exporse - integral * Double.valueOf(coModel.getTotal().getIntegral_scale()) - band * Double.valueOf(coModel.getTotal().getBond_scale()));
                                txtTotal.setText(String.valueOf(c.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()));
                            } else {
                                BigDecimal c = new BigDecimal(b.doubleValue() + totleprice + fee - integral * Double.valueOf(coModel.getTotal().getIntegral_scale()) - band * Double.valueOf(coModel.getTotal().getBond_scale()));
                                txtTotal.setText(String.valueOf(c.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()));
                            }
                        }

                    }

                }
                break;
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_Left:
                finish();
                break;
            //选择收货地址
            case R.id.layoutAddress:
                Intent itA = new Intent(context, AddressListActivity.class);
                itA.putExtra("order", true);
                startActivityForResult(itA, 1);
                break;


            case R.id.txtButton:

                /*count();*/
                break;
            //提交订单
            case R.id.txtSubmit:

                if (exporseShow && exporse != 0) {
                    final NumberSelectDialog pwddialog = new NumberSelectDialog(context);
                    pwddialog.setOnPositiveListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {

                            pwd = pwddialog.getnum();

                            pwddialog.dismiss();
                            submitExamine();
                        }
                    });
                    pwddialog.show();
                } else {
                    pwd = "";

                    submitExamine();
                }


                break;

            case R.id.ll_creatoeder_expenseshow:
                if (exporseShow) {
                    exporseShow = false;
                    iv_expense.setImageDrawable(getResources().getDrawable(R.drawable.select_no));
                    ll_expensein.setVisibility(View.GONE);
                    exporse = 0;
                    tv_extre.setText("使用余额:" + exporse);
                    BigDecimal b = null;
                    if (hasdill) {
                        b = new BigDecimal(Double.valueOf(tv_dillorice.getText().toString()) + totleprice + fee - exporse - integral * Double.valueOf(coModel.getTotal().getIntegral_scale()) - band * Double.valueOf(coModel.getTotal().getBond_scale()));
                    } else {
                        b = new BigDecimal(totleprice + fee - exporse - integral * Double.valueOf(coModel.getTotal().getIntegral_scale()) - band * Double.valueOf(coModel.getTotal().getBond_scale()));
                    }

                    txtTotal.setText(String.valueOf(b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()));
                    if(integral * Double.valueOf(coModel.getTotal().getIntegral_scale())==0)
                    {
                        setIntegralVisiable(View.VISIBLE);
                    }

                    if (band * Double.valueOf(coModel.getTotal().getBond_scale()) == 0){
                        setBandVisiable(View.VISIBLE);
                    }
                } else {
                    giveIntegral.setVisibility(View.GONE);
                    exporseShow = true;
                    iv_expense.setImageDrawable(getResources().getDrawable(R.drawable.select_yes));
                    ll_expensein.setVisibility(View.VISIBLE);
                    if (et_expenseV.getText().toString().equals("")) {
                        exporse = 0;
                    } else {
                        exporse = Double.valueOf(et_expenseV.getText().toString());
                    }
                    tv_extre.setText("使用余额:" + exporse);
                    BigDecimal b = null;
                    if (hasdill) {
                        b = new BigDecimal(Double.valueOf(tv_dillorice.getText().toString()) + totleprice + fee - exporse - integral * Double.valueOf(coModel.getTotal().getIntegral_scale()) - band * Double.valueOf(coModel.getTotal().getBond_scale()));
                    } else {
                        b = new BigDecimal(totleprice + fee - exporse - integral * Double.valueOf(coModel.getTotal().getIntegral_scale()) - band * Double.valueOf(coModel.getTotal().getBond_scale()));
                    }
                    txtTotal.setText(String.valueOf(b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()));
                }
                break;
            case R.id.ll_creatorder_hoss:
                if (hossShow) {
                    hossShow = false;
                    ll_hossway.setVisibility(View.GONE);
                } else {
                    hossShow = true;
                    ll_hossway.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.ll_creatoeder_hosswait:
                ChHossWay(0);
                hossway = 0;
                break;
            case R.id.ll_creatoeder_hosscancle:
                ChHossWay(1);
                hossway = 1;
                break;
            case R.id.ll_creatoeder_hosschar:
                ChHossWay(2);
                hossway = 2;
                break;
            case R.id.tv_createorder_dill:
                Intent dillIntent = new Intent(mContext, DillDetailActivity.class);
                startActivityForResult(dillIntent, 2);
                break;
        }
    }

    private void submitExamine() {

        if (paytypeid < 0 && Double.valueOf(txtTotal.getText().toString()) != 0) {

            Toast.makeText(context, "请选择支付方式", Toast.LENGTH_LONG).show();
            return;
        }
        if (addressID.equals("0")) {
            Toast.makeText(context, "请添加一个收货地址", Toast.LENGTH_LONG).show();
            return;
        }


        List<OrderGlist> OG = coModel.getGoods_list();
        String integralstr = "";
        String bondStr = "";
        String transtr = "";

        for (int i = 0; i < OG.size(); i++) {
            if(OG.get(i).getGoodlist() == null || OG.get(i).getShipping_list() ==null){
                continue;
            }
            if (i != OG.size() - 1) {
                transtr = transtr + OG.get(i).getGoodlist().get(0).getSupplier_id() + "-" + OG.get(i).getShipping_list().get(0).getShipping_id() + ",";
            } else {
                transtr = transtr + OG.get(i).getGoodlist().get(0).getSupplier_id() + "-" + OG.get(i).getShipping_list().get(0).getShipping_id();
            }
        }
        for (int i = 0; i < OG.size(); i++) {
            if(OG.get(i).getGoodlist() == null){
                continue;
            }
            if (i != OG.size() - 1) {
                if (OG.get(i).getUseintegral() == null) {
                    integralstr = integralstr + OG.get(i).getGoodlist().get(0).getSupplier_id() + "-" + "0,";
                } else {
                    integralstr = integralstr + OG.get(i).getGoodlist().get(0).getSupplier_id() + "-" + OG.get(i).getUseintegral() + ",";
                }

                if (OG.get(i).getUserBand() == null){
                    bondStr = bondStr + OG.get(i).getGoodlist().get(0).getSupplier_id() + "-" + "0,";
                }else{
                    bondStr = bondStr + OG.get(i).getGoodlist().get(0).getSupplier_id() + "-" + OG.get(i).getUserBand() + ",";
                }

            } else {
                if (OG.get(i).getUseintegral() == null) {
                    integralstr = integralstr + OG.get(i).getGoodlist().get(0).getSupplier_id() + "-" + "0";
                } else {
                    integralstr = integralstr + OG.get(i).getGoodlist().get(0).getSupplier_id() + "-" + OG.get(i).getUseintegral();
                }

                if (OG.get(i).getUserBand() == null) {
                    bondStr = bondStr + OG.get(i).getGoodlist().get(0).getSupplier_id() + "-" + "0";
                } else {
                    bondStr = bondStr + OG.get(i).getGoodlist().get(0).getSupplier_id() + "-" + OG.get(i).getUserBand();
                }
            }
        }

        if (itType.equals("1")) {
            if (hasdill) {
                soPresenter.requestInfogoodsDill(context, exporse + "", getIntent().getStringExtra("Carid"), addressID, paytypeid + "", integralstr,bondStr, transtr, editContent.getText().toString(), hossway + "", txtTotal.getText().toString(), pwd, bean, oversea, overseaStr);
            } else {
                soPresenter.requestInfogoods(context, exporse + "", getIntent().getStringExtra("Carid"), addressID, paytypeid + "", integralstr,bondStr, transtr, editContent.getText().toString(), hossway + "", txtTotal.getText().toString(), pwd, oversea, overseaStr);
            }

        } else {
            if (hasdill) {
                soPresenter.requestdill(context, exporse + "", addressID, paytypeid + "", integralstr,bondStr, transtr, editContent.getText().toString(), hossway + "", txtTotal.getText().toString(), pwd, bean, oversea, overseaStr);
            } else {
                soPresenter.request(context, exporse + "", addressID, paytypeid + "", integralstr,bondStr, transtr, editContent.getText().toString(), hossway + "", txtTotal.getText().toString(), pwd, oversea, overseaStr);
            }

        }


    }

    private CreateOrder coModel = new CreateOrder();


    private void setIntegralVisiable(int visiable) {
        if (giveIntegral.getVisibility() == visiable) return;
        else giveIntegral.setVisibility(visiable);
    }

    private void setBandVisiable(int visiable) {
        if (giveBand.getVisibility() == visiable) return;
        else giveBand.setVisibility(visiable);
    }


    /**
     * 订单信息返回
     *
     * @param model
     */


    @Override
    public void getOrderInfor(CreateOrderModel model) {
        switch (Integer.parseInt(model.getCode())) {
            case 0:
                Utils.showToast(context, model.getMsg());
                CreateOrderActivity.this.findViewById(R.id.rootContent).setVisibility(View.GONE);
                break;
            case 1:
                coModel = model.getData();
                txtAddress.setText(coModel.getUser_consignee().getSub_consignee());
                txtPhone.setText(coModel.getUser_consignee().getMobile());
                txtName.setText(coModel.getUser_account().getUser_name());
                txtGoodPrice.setText(coModel.getTotal().getGoods_price_formated());
                totleprice = Double.valueOf(coModel.getTotal().getGoods_price_formated());
                txtFee.setText(coModel.getTotal().getShipping_fee_formated());
                fee = Double.valueOf(coModel.getTotal().getShipping_fee_formated());
                txtTotal.setText(String.valueOf(totleprice + fee));
                giveIntegral.setText("*该订单完成后，您将获得" + coModel.getTotal().getWill_get_integral() + "消费券");

                showData();
                break;
            case 3:
                Utils.showDialog(this, "提示", model.getMsg(), "", "", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
                break;
            //没有收货地址去选择
            case 4:
                coModel = model.getData();
                showData();
                Intent intent = new Intent(context, AddressListActivity.class);
                intent.putExtra("order", true);
                startActivityForResult(intent, 1);
                Utils.showToast(context, model.getMsg());
                break;
            case -220:
                Utils.showToast(context, model.getMsg());
                finish();
                break;
        }
    }


    /**
     * 展示订单信息
     */
    private void showData() {
        addressID = coModel.getUser_consignee().getAddress_id();


        if (Double.valueOf(coModel.getUser_account().getUser_surplus()) <= 0) {
            ll_expenseshow.setVisibility(View.GONE);
        } else {
            ll_expenseshow.setVisibility(View.VISIBLE);
        }
        tv_expenseall.setText("您当前可用的余额为：" + coModel.getUser_account().getUser_surplus());
        final PayTypeAdapter paytypeadapter = new PayTypeAdapter(this, R.layout.paytype_item);
        lv_paytype.setAdapter(paytypeadapter);
        paytypeadapter.addAll(coModel.getPayment());
        lv_paytype.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                if (paytypenum < 0) {
                    paytypeadapter.clear();
                    paytypenum = position;
                    coModel.getPayment().get(position).setChecked(true);
                    paytypeid = coModel.getPayment().get(position).getPay_id();
                    paytypeadapter.addAll(coModel.getPayment());

                } else {
                    if (!coModel.getPayment().get(position).isChecked()) {
                        paytypeadapter.clear();
                        coModel.getPayment().get(paytypenum).setChecked(false);
                        paytypenum = position;
                        coModel.getPayment().get(position).setChecked(true);
                        paytypeid = coModel.getPayment().get(position).getPay_id();

                        paytypeadapter.addAll(coModel.getPayment());
                    }

                }
            }
        });
        ShowGoods(coModel.getGoods_list(), coModel.getUser_account().getUser_integral(),coModel.getUser_account().getUser_bond());
        et_expenseV.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                double dill=0.00;
                if(hasdill)
                {
                    dill=Double.valueOf(tv_dillorice.getText().toString());
                }


                if (editable.toString().equals("")) {
                    exporse = 0;
                    tv_extre.setText("使用余额:" + exporse);
                    if (hasdill) {
                        txtTotal.setText(String.valueOf(Double.valueOf(tv_dillorice.getText().toString()) + totleprice + fee - exporse - integral * Double.valueOf(coModel.getTotal().getIntegral_scale()) - band * Double.valueOf(coModel.getTotal().getBond_scale())));
                    } else {
                        txtTotal.setText(String.valueOf(totleprice + fee - exporse - integral * Double.valueOf(coModel.getTotal().getIntegral_scale()) - band * Double.valueOf(coModel.getTotal().getBond_scale())));
                    }

                } else if (Double.valueOf(editable.toString()) > Double.valueOf(coModel.getUser_account().getUser_surplus()) || Double.valueOf(editable.toString()) > dill+totleprice + fee - integral * Double.valueOf(coModel.getTotal().getIntegral_scale() ) - band * Double.valueOf(coModel.getTotal().getBond_scale())) {
                    Toast.makeText(context, "余额不能超过使用范围", Toast.LENGTH_LONG).show();
                    et_expenseV.setText("0");
                } else {
                    exporse = Double.valueOf(editable.toString());
                    tv_extre.setText("使用余额:" + exporse);
                    if (hasdill) {
                        BigDecimal b = new BigDecimal(Double.valueOf(tv_dillorice.getText().toString()) + totleprice + fee - exporse - integral * Double.valueOf(coModel.getTotal().getIntegral_scale()) - band * Double.valueOf(coModel.getTotal().getBond_scale()));
                        txtTotal.setText(String.valueOf(b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()));
                    } else {
                        BigDecimal b = new BigDecimal(totleprice + fee - exporse - integral * Double.valueOf(coModel.getTotal().getIntegral_scale()) - band * Double.valueOf(coModel.getTotal().getBond_scale()));
                        txtTotal.setText(String.valueOf(b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()));
                    }

                }

                if (!TextUtils.isEmpty(et_expenseV.getText().toString())) {
                    setIntegralVisiable(View.GONE);
                } else {
                    //用户天河积分不为0时 显示天河积分
                    if (!TextUtils.equals(coModel.getUser_account().getUser_integral(),"0") && TextUtils.isEmpty(et_useintegral.getText().toString()))
                        setIntegralVisiable(View.VISIBLE);
                }
            }
        });



    }

    //提交订单返回
    @Override
    public void result(SubmitOrderModel model) {
        if (model.getCode().equals("1")) {
            if (Double.valueOf(txtTotal.getText().toString()) == 0)
            {
                Intent itOrder = new Intent(mContext, MyOrderActivity.class);
                startActivity(itOrder);
                finish();
                return;
            }
            Intent intent = new Intent(context, OrderSureActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("ordermsg", model.getData());
            intent.putExtras(bundle);

            startActivity(intent);
            finish();
        } else if (model.getCode().equals("-300")) {
            final NumberSelectDialog pwddialog2 = new NumberSelectDialog(context);
            pwddialog2.setOnPositiveListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    pwd = pwddialog2.getnum();

                    pwddialog2.dismiss();
                    submitExamine();
                }
            });
            pwddialog2.show();
        }
        Utils.showToast(context, model.getMsg());
    }

    private EditText et_useintegral,et_user_band;

    /**
     * 展示订单商品信息
     *
     * @param goodslist
     * @param userIntegral
     */
    public void ShowGoods(final List<OrderGlist> goodslist, final String userIntegral,final String userBand) {

        LayoutInflater inflater = LayoutInflater.from(context);

        for (int i = 0; i < goodslist.size(); i++) {

            final OrderGlist goodsship = goodslist.get(i);

            int shipIntegral = 0;
            float shipBand = 0;
            View view = inflater.inflate(R.layout.creatorder_goods_item, null);

            TextView tv_ship = (TextView) view.findViewById(R.id.tv_ogitem_shipname);
            final RelativeLayout rl_transport = (RelativeLayout) view.findViewById(R.id.rlDistribution);
            MyListView lv_good = (MyListView) view.findViewById(R.id.lv_ogitem_goods);
            TextView tv_transportdetail = (TextView) view.findViewById(R.id.tv_ogitem_transportdetail);
            TextView tv_transportname = (TextView) view.findViewById(R.id.tv_ogitem_transportname);
            RelativeLayout rl_integral = (RelativeLayout) view.findViewById(R.id.rl_ogi_integral);
            RelativeLayout rl_band = (RelativeLayout) view.findViewById(R.id.rl_ogi_band);
            RelativeLayout rl_hv = (RelativeLayout) view.findViewById(R.id.rl_createorder_hv);
            final MyListView lv_hvpay = (MyListView) view.findViewById(R.id.lv_creatorder_hvpaytype);
            final PayTypeAdapter hvadapter = new PayTypeAdapter(this, R.layout.paytype_item);
            lv_hvpay.setVisibility(View.GONE);
            if (goodsship.getPay_set() == 1) {
                oversea = true;
                rl_hv.setVisibility(View.VISIBLE);
                lv_hvpay.setAdapter(hvadapter);
                hvadapter.addAll(goodsship.getPayments());
                overseaStr = goodsship.getGoodlist().get(0).getSupplier_id() + "-" + goodsship.getPayments().get(0).getId();
            } else {
                rl_hv.setVisibility(View.GONE);
            }
            final int[] hvnum = {1};
            lv_hvpay.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

                    if (!goodsship.getPayments().get(position).isChecked()) {
                        hvadapter.clear();
                        goodsship.getPayments().get(hvnum[0]).setChecked(false);
                        hvnum[0] = position;
                        goodsship.getPayments().get(position).setChecked(true);
                        // paytypeid=coModel.getPayment().get(position).getPay_id();
                        overseaStr = goodsship.getGoodlist().get(0).getSupplier_id() + "-" + goodsship.getPayments().get(position).getId();
                        hvadapter.addAll(goodsship.getPayments());
                    }


                }
            });

            final int[] hv = {1};
            rl_hv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (hv[0] == 0) {
                        lv_hvpay.setVisibility(View.GONE);
                        hv[0] = 1;
                    } else {
                        lv_hvpay.setVisibility(View.VISIBLE);
                        hv[0] = 0;
                    }
                }
            });

            quickAdapter = new QuickAdapter<CartGoods>(context, R.layout.order_good_item) {
                @Override
                protected void convert(BaseAdapterHelper helper, CartGoods item) {
                    Picasso.with(context).load(ConfigValue.IMG_IP + item.getGoods_img())
                            .error(R.mipmap.ic_launcher)
                            .into((ImageView) helper.getView(R.id.imgGoods));
                    helper.setText(R.id.txtTitle, item.getGoods_name());
                    helper.setText(R.id.txtPrice, "￥" + item.getGoods_price());
                    helper.setText(R.id.txtNumber, "数量：" + item.getNumber());
                }
            };


            lv_good.setAdapter(quickAdapter);
            quickAdapter.addAll(goodslist.get(i).getGoodlist());

            if (!userIntegral.equals("0")) {
                for (int j = 0; j < goodslist.get(i).getGoodlist().size(); j++) {
                    shipIntegral = shipIntegral + Integer.valueOf(goodslist.get(i).getGoodlist().get(j).getIntegral());
                    shipBand = shipBand + Float.valueOf(goodslist.get(i).getGoodlist().get(j).getMax_bond());
                }

                if (shipIntegral == 0) {
                    rl_integral.setVisibility(View.GONE);
                } else {
                    if (goodsship.getPay_set() != 1) {
                        rl_integral.setVisibility(View.VISIBLE);
                    } else {
                        rl_integral.setVisibility(View.VISIBLE);
                    }

                    final TextView tv_shipintegral = (TextView) view.findViewById(R.id.tv_ogi_integral);
                    tv_shipintegral.setText(shipIntegral + "");
                    et_useintegral = (EditText) view.findViewById(R.id.et_cop_userin);
                    et_useintegral.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                        }

                        @Override
                        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                        }

                        @Override
                        public void afterTextChanged(Editable editable) {
                            int z = 0;
                            if (Integer.valueOf(tv_shipintegral.getText().toString()) <= Integer.valueOf(userIntegral)) {
                                z = Integer.valueOf(tv_shipintegral.getText().toString());
                            }
                            if (editable.toString().equals("")) {
                                goodsship.setUseintegral("0");
                                count();
                            } else if (Integer.valueOf(editable.toString()) > z) {
                                Toast.makeText(context, "积分不能超过使用范围", Toast.LENGTH_LONG).show();
                                et_useintegral.setText("0");
                            } else {
                                goodsship.setUseintegral(editable.toString());
                                count();
                            }

                            if (!TextUtils.isEmpty(et_useintegral.getText().toString())) {
                                setIntegralVisiable(View.GONE);
                            } else {
                                if (TextUtils.isEmpty(et_expenseV.getText().toString()))
                                    setIntegralVisiable(View.VISIBLE);
                            }
                        }
                    });

                }


                if (shipBand == 0) {
                    rl_band.setVisibility(View.GONE);
                } else {
                    if (goodsship.getPay_set() != 1) {
                        rl_band.setVisibility(View.VISIBLE);
                    } else {
                        rl_band.setVisibility(View.VISIBLE);
                    }

                    final TextView tv_shipBand = (TextView) view.findViewById(R.id.tv_ogi_band);
                    tv_shipBand.setText(shipBand + "");
                    et_user_band = (EditText) view.findViewById(R.id.et_cop_userin_band);
                    et_user_band.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                        }

                        @Override
                        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                        }

                        @Override
                        public void afterTextChanged(Editable editable) {
                            float z = 0;
                            if (Float.valueOf(tv_shipBand.getText().toString()) <= Float.valueOf(userBand)) {
                                z = Float.valueOf(tv_shipBand.getText().toString());
                            }
                            if (editable.toString().equals("")) {
                                goodsship.setUserBand("0");
                                count();
                            } else if (Float.valueOf(editable.toString()) > z) {
                                Toast.makeText(context, "消费券不能超过使用范围", Toast.LENGTH_LONG).show();
                                et_user_band.setText("0");
                            } else {
                                goodsship.setUserBand(editable.toString());
                                count();
                            }

                            if (!TextUtils.isEmpty(et_user_band.getText().toString())) {
                                setBandVisiable(View.GONE);
                            } else {
                                if (TextUtils.isEmpty(et_expenseV.getText().toString()))
                                    setBandVisiable(View.VISIBLE);
                            }
                        }
                    });

                }
            }


            tv_ship.setText(goodslist.get(i).getGoodlist().get(0).getSeller());
            rl_transport.setTag(goodslist.get(i));

            if(goodslist.get(i).getShipping_list() != null && goodslist.get(i).getShipping_list().size() > 0){
                tv_transportdetail.setText(goodslist.get(i).getShipping_list().get(0).getShipping_desc());
                tv_transportname.setText(goodslist.get(i).getShipping_list().get(0).getShipping_name());
            }



            ll_goods.addView(view);
        }
    }


    public void count() {
        integral = 0;
        band = 0;

        List<OrderGlist> shipgoods = coModel.getGoods_list();

        for (int i = 0; i < shipgoods.size(); i++) {
            if (shipgoods.get(i).getUseintegral() != null) {
                integral = integral + (shipgoods.get(i).getUseintegral() == null ? 0 : Integer.valueOf(shipgoods.get(i).getUseintegral()));
                band = band + (shipgoods.get(i).getUserBand() == null ? 0 : Float.valueOf(shipgoods.get(i).getUserBand()));
            }

        }
        tv_instr.setText("使用积分:" + integral * Double.valueOf(coModel.getTotal().getIntegral_scale()));
        tv_band.setText("使用消费券:" + band * Double.valueOf(coModel.getTotal().getBond_scale()));

        if (hasdill) {

            BigDecimal b = new BigDecimal(Double.valueOf(tv_dillorice.getText().toString()) + totleprice + fee - exporse - integral * Double.valueOf(coModel.getTotal().getIntegral_scale()) - band * Double.valueOf(coModel.getTotal().getBond_scale()));
            txtTotal.setText(String.valueOf(b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()));
        } else {
            BigDecimal b = new BigDecimal(Double.valueOf(totleprice + fee - exporse - integral * Double.valueOf(coModel.getTotal().getIntegral_scale()) - band * Double.valueOf(coModel.getTotal().getBond_scale())));
            txtTotal.setText(String.valueOf(b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()));
        }


    }

    public void ChHossWay(int wayNo) {

        switch (wayNo) {
            case 0:
                iv_hosswait.setImageDrawable(getResources().getDrawable(R.drawable.select_yes));
                iv_hosscancle.setImageDrawable(getResources().getDrawable(R.drawable.select_no));
                iv_hosschat.setImageDrawable(getResources().getDrawable(R.drawable.select_no));
                break;
            case 1:
                iv_hosswait.setImageDrawable(getResources().getDrawable(R.drawable.select_no));
                iv_hosscancle.setImageDrawable(getResources().getDrawable(R.drawable.select_yes));
                iv_hosschat.setImageDrawable(getResources().getDrawable(R.drawable.select_no));
                break;
            case 2:
                iv_hosswait.setImageDrawable(getResources().getDrawable(R.drawable.select_no));
                iv_hosscancle.setImageDrawable(getResources().getDrawable(R.drawable.select_no));
                iv_hosschat.setImageDrawable(getResources().getDrawable(R.drawable.select_yes));
                break;
        }
    }

    public class NumberSelectDialog extends Dialog {

        private Context context;
        private Button btn_sure;
        private Button btn_cencle;
        private EditText et_number;

        private int pnum;

        public NumberSelectDialog(Context context) {
            super(context, R.style.Theme_AppCompat_Dialog);
            // TODO Auto-generated constructor stub
            this.context = context;

            setdialog();
        }

        public void setdialog() {
            View view = LayoutInflater.from(context).inflate(
                    R.layout.numberselect_dialog, null);
            btn_cencle = (Button) view.findViewById(R.id.nsd_btn_cencle);

            et_number = (EditText) view.findViewById(R.id.nsd_et_num);

            super.setContentView(view);

        }

        public String getnum() {

            return et_number.getText().toString();


        }


        /**
         * 确定键监听器
         *
         * @param listener
         */

        public void setOnPositiveListener(View.OnClickListener listener) {
            btn_cencle.setOnClickListener(listener);
        }


    }


}
